I am in my battle royale arena and now we have to make it so that the last person standing is declared
the winner.
They are given some sort of prize and then they're teleported back.
Let's go to our battle manager under server script service and take a look at that.
Let's go to our variable section up at the top, make a flag to determine whether the battle is ended
or not.
I'm going to say is ended and I'm going to initialize it to false.
Now, in the battle itself, after we tell people, hey, it's begun, so run, hide, start shooting,
whatever you get your weapons, let's make a while loop.
Let's say while not is ended, do let's put a wait statement in there so we don't we don't exhaust our
resources.
Let me just scroll this code up a little bit.
There we go.
And we'll continue.
Let's get the number of players, local num equals number of players.
But that's a service.
We have to get the children in the service to get the number and then I'll check to see if the number
of players equals equals one.
If it does, we have a last person standing.
Let's go ahead and get that player.
Local player.
I'll say players get children.
I probably should have made an array out of this so that I wouldn't have to do something weird like
this.
So it's going to get the first element in this array, right?
I don't usually like to combine stuff like that, but I'll do it here.
Let's do an if statement just to make sure the player exists.
Although this would probably you would probably error if this didn't work and then this if statement
would be irrelevant.
But let's put it in there just in case we'll get an is ended equals true, right?
Because there's one player left.
The battle ended.
We don't want to check anymore.
And then let's do a print statement.
Right.
See if things are things are printing out properly.
We'll do winner and the player.name.
And then what should we give the player if they win?
Let's do player leader stats points dot value plus equals 200.
We'll give them 200 points on the leaderboard on the leader stats and then our game manager is saving
stuff off on this side too, so we shouldn't have a problem with it.
And we have it for the player removing event two or saving things off.
Let's do our status RE to the client side so we get a little a little message, a little banner popping
up player.
So then we're going to send in two parameters.
Our battle info, which was the client side script for our little UI, is expecting two parameters.
You don't have to worry about this one.
This player right here just says, Hey, fire client to this player.
That's not going to show up in your battle info log script, but you are going to get a status.
Remember, we'll take a look at it a minute.
We have a win.
It's begin, prepare and win.
Those were the statuses are if conditions we're looking for and then we're going to have our message
you are the winner.
How about that?
You are the winner.
Yay.
Cool.
And then what else do we need to do?
So we sent our message.
We're going to get the UI pop up.
Let's wait three seconds so that you can look at it, Make it five if you want.
Maybe give them a little time to bask in their glory.
But after we wait, we want to teleport back.
So we're going to use the teleport service and then we'll do our teleport.
We're going to get our home ID, and then we'll get our player to send back.
There's only one player left.
Let's send them back.
But I want to test this out in Roblox studio.
Teleport is no bueno for studio, so I'm going to do a little trick.
I'm going to scroll up to the top.
In the variable section here, and I'm going to get a variable for the run service run SVR.
Let's call it that game.
Get service, run service.
And then I could check to see if I'm in studio.
So we come down here, do a little if statement.
If not, run service is studio.
Then we'll teleport.
Otherwise we'll just skip over it because we have to test it out.
And then here's our here is our number of players equals one.
Let's get down here.
Maybe do a little print out.
Should we do print out?
We could do a print out.
It won't hurt anything.
We'll do there.
Whoops.
There is.
There are.
And then we'll do the number of players.
Right that way, if there's if there's a problem, we can kind of see what's going on.
It's good to have print outs, especially in your debugging.
All right.
This is pretty cool.
I think it's ready to give a dry run in studio.
Let's go to play.
See what we got.
Eight, seven, six, five.
That's pretty cool.
Let the games begin.
And I'm the winner because I'm the last player.
And I didn't even even check to see if I got more points.
I'm assuming I did.
I'll check.
I'll check before I publish the video, but that's pretty cool.
And then we didn't get an error for our.
There are one players.
We didn't get an error because we skipped right over our teleport because we are in studio.
Let's go ahead and try this on Roblox.
So I got my alt right here.
He's on the phone, so it might take a while to bumble around with that.
If you'll notice, the points are correct and they saved off.
So that's cool because we do have saving on the arena side of things.
Start our battle.
I'm going to accept I'm going to pick up my phone here and I'm going to hit the accept for my alt.
We're both going to end up in the other world and my ALT has 90 points.
Let's have oh, he doesn't have a gun.
It's going to say, let's have him win.
But he can't.
Here we go.
Cool.
Hey, there he is.
Hey, man, What are you doing here?
We're going to shoot you.
Let the games begin.
Get my gun.
Oh, man, I should have cranked that up.
I paused the video because it was healing almost as fast as the gun was was hurting him.
There we go.
Ha.
Yeah.
And we're going back.
That's pretty cool.
Let's make sure our alts there he is nice.
And he didn't.
He didn't win anything.
We got an extra kill and he got an extra death.